<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/cafe/_init_model_cafe.php';
		
	class mod_cafe_img_cover extends _init_model_cafe
	{		
		public $CAF_IMG_C_ID;
		public $CAF_IMG_C_FILENAME;
		public $CAF_ID;
		
		function mod_cafe_img_cover(){
			parent::__construct();
			
			$this->tableName = "cafe_img_cover";
		}
		
		function get_img_cover_filename($img_cover_id) {
			
			$sqlcmd = 'SELECT CAF_IMG_C_FILENAME';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE CAF_IMG_C_ID = ' . $img_cover_id;
			
			$query = $this->db->query($sqlcmd);
			
			$this->load->library('define/directory_path');
			if ($query->num_rows() > 0)			
				return $this->directory_path->img_cover_path . $query->row(0)->CAF_IMG_C_FILENAME;			
			else
				return $this->directory_path->img_no_cover_path;
		}
		function get_cover_list($cafe_id=null, $count=null, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);

			$this->db->order_by('CAF_IMG_C_ID', 'DESC');
			if (!is_null($count))
				$query = $this->db->get($this->tableName, $count);
			else
				$query = $this->db->get($this->tableName);
				
			$return = $query->result_array();
				
			if (count($return) > 0){
				$this->load->library('define/directory_path');
						
				for ($i=0; $i<count($return); $i++){
					if (isset($return[$i]['CAF_IMG_C_FILENAME'])){
						if (!empty($return[$i]['CAF_IMG_C_FILENAME']))
							$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return[$i]['CAF_IMG_C_FILENAME'];
						else
							$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_no_cover_path;
					}
				}				
			}
				
			return $return;
		}
		function get_current_img_count($cafe_id=null){
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
			return $this->db->count_all_results($this->tableName);
		}
		function get_id_by_filename($filename){
			$this->db->select(array('CAF_IMG_C_ID'));
			$this->db->where('CAF_IMG_C_FILENAME', $filename);
			$query = $this->db->get($this->tableName);
				
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_IMG_C_ID;
			else
				return null;
		}
	
		function add(){
			$data = array('CAF_IMG_C_FILENAME' => $this->CAF_IMG_C_FILENAME,
							'CAF_ID' => $this->CAF_ID);
			return $this->db->insert($this->tableName, $data);			
		}
		function remove_by_filename($filename){
			$this->db->where('CAF_IMG_C_FILENAME', $filename);
			if ($this->db->delete($this->tableName)){
				$this->load->library('define/directory_path');
				unlink($this->directory_path->img_upload_cover_path . $filename);
				
				return true;
			} else 
				return false;
			
		}
		function remove_by_cafe($cafe_id){			
			$result = $this->get_cover_list($cafe_id, null, array('CAF_IMG_C_FILENAME'));

			$this->load->library('define/directory_path');
			for ($i=0; $i<count($result); $i++){
				unlink($this->directory_path->img_upload_cover_path . $result[$i]['CAF_IMG_C_FILENAME']);
			}

			return parent::remove_by_cafe($cafe_id);
		}
	}
?>